package com.telra.app.server2.service;

import com.telra.app.server2.config.CassandraConfig;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.factory.HFactory;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Log4JLogger;

/**
 * User: jamesarlet
 * Date: 1/16/12
 * Time: 6:54 PM
 */
public class CassandraService {

    private final static Log4JLogger log = (Log4JLogger) LogFactory.getFactory().getInstance(CassandraService.class);

    private CassandraConfig config;

    private Cluster cluster;
    private Keyspace keyspace;

    public CassandraService() {
        //empty
    }

    private void init()  {
        log.info("Connecting to cluster " + config.getCassandraCluster());
        this.cluster = HFactory.getOrCreateCluster(config.getCassandraCluster(), config.getCassandraHost() +
                ":" + config.getCassandraPort());
        log.info("Connecting to keyspace " + config.getCassandraKeyspace());
        this.keyspace = HFactory.createKeyspace(config.getCassandraKeyspace(), cluster);
        log.info("Cassandra initialized");
    }

    public CassandraConfig getConfig() {
        return config;
    }

    public void setConfig(CassandraConfig config) {
        this.config = config;
        init();
    }

    public Keyspace getKeyspace() {
        return keyspace;
    }

    public Cluster getCluster() {
        return cluster;
    }

}